<?php

class Config {
    private static array $config = [
        'suits' => ['黑桃', '红桃', '梅花', '方片'],
        'ranks' => ['2','3','4','5','6','7','8','9','10','J','Q','K','A'],
        'rank_values' => [
            '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8,
            '9' => 9, '10' => 10, 'J' => 11, 'Q' => 12, 'K' => 13, 'A' => 14
        ],

        'prop_types' => [
            ['type' => 'card_point_boost', 'name' => '指定${num}张牌的点数翻倍', 'key' => ['card' => '${card}']],
            ['type' => 'hand_multiplier', 'name' => '指定${hand_type}牌型增加${rote}倍倍率', 'key' => ['hand_type' => '${hand_type}', 'rote' => '${rote}']],
            ['type' => 'card_replacement', 'name' => '指定${num}张牌替换牌池中的牌', 'key' => ['num' => '${num}']],
            ['type' => 'card_double', 'name' => '随机选择${num}张牌的点数翻${rote}倍倍率', 'key' => ['num' => '${num}', 'rote' => '${rote}']],
            ['type' => 'suit_double', 'name' => '指定牌的${suits}花色点数翻${rote}倍倍率', 'key' => ['suits' => '${suits}', 'rote' => '${rote}']],
            ['type' => 'extra_play', 'name' => '增加${num}次出牌机会', 'key' => ['num' => '${num}']],
            ['type' => 'extra_discard', 'name' => '增加${num}次弃牌机会', 'key' => ['num' => '${num}']],
            ['type' => 'critical_strike', 'name' => '合得分翻${rote}倍倍率', 'key' => ['rote' => '${rote}']]
        ],

        'hand_type' => [
            '高牌' => 1,
            '对子' => 1.5,
            '两对' => 2,
            '三条' => 5,
            '顺子' => 10,
            '同花' => 15,
            '葫芦' => 20,
            '四条' => 50,
            '同花顺' => 100
        ]
    ];


    /**
     * 获取动态生成的道具
     * @param string $propType 道具类型（如 'card_point_boost'）
     * @param int|null $num 数量（可选）
     * @param int|null $rote 倍率（可选）
     * @param string|null $suits 花色（可选）
     * @param string|null $handType 牌型（可选）
     * @return array 包含道具名称和参数的数组
     * @throws InvalidArgumentException 如果道具类型不存在
     */
    public static function getProp(
        string $propType,
        ?int $num = null,
        ?int $rote = null,
        ?string $suits = null,
        ?string $handType = null
    ): array {
        // 获取道具配置
        $propTypes = self::get('prop_types');
        $propConfig = null;

        // 查找匹配的道具类型
        foreach ($propTypes as $prop) {
            if ($prop['type'] === $propType) {
                $propConfig = $prop;
                break;
            }
        }

        if ($propConfig === null) {
            throw new InvalidArgumentException("道具类型 {$propType} 不存在");
        }

        $name = $propConfig['name'];
        $key = $propConfig['key'];

        // 替换占位符
        foreach ($key as $placeholder => $value) {
            switch ($placeholder) {
                case 'num':
                    if ($num !== null) {
                        $name = str_replace('${num}', $num, $name);
                        $key[$placeholder] = $num;
                    }
                    break;
                case 'rote':
                    if ($rote !== null) {
                        $name = str_replace('${rote}', $rote, $name);
                        $key[$placeholder] = $rote;
                    }
                    break;
                case 'suits':
                    if ($suits !== null) {
                        $name = str_replace('${suits}', $suits, $name);
                        $key[$placeholder] = $suits;
                    }
                    break;
                case 'hand_type':
                    if ($handType !== null) {
                        $name = str_replace('${hand_type}', $handType, $name);
                        $key[$placeholder] = $handType;
                    }
                    break;
            }
        }

        return [
            'type' => $propType,
            'name' => $name,
            'key' => $key
        ];
    }

    /**
     * 获取动态生成的组合道具
     * @param array $propTypes 道具类型数组（如 ['extra_play', 'extra_discard']）
     * @param array $params 参数数组，格式为 ['extra_play' => ['num' => 1], 'extra_discard' => ['num' => 1]]
     * @return array 包含组合道具名称和参数的数组
     * @throws InvalidArgumentException 如果道具类型不存在
     */
    public static function getCombinedProp(array $propTypes, array $params = []): array {
        $combinedName = '';
        $combinedKey = [];

        foreach ($propTypes as $propType) {
            // 获取单个道具配置
            $prop = self::getProp(
                $propType,
                $params[$propType]['num'] ?? null,
                $params[$propType]['rote'] ?? null,
                $params[$propType]['suits'] ?? null,
                $params[$propType]['hand_type'] ?? null
            );

            // 合并名称和参数
            $combinedName .= $prop['name'] . ' + ';
            $combinedKey = array_merge($combinedKey, $prop['key']);
        }

        // 移除末尾的 " + "
        $combinedName = rtrim($combinedName, ' + ');

        return [
            'type' => implode('+', $propTypes),
            'name' => $combinedName,
            'key' => $combinedKey,
        ];
    }



    /**
     * 获取配置
     * @param $key
     * @return array
     */
    public static function get($key): array
    {
        return self::$config[$key] ?? [];
    }
}
